import { getAction } from "@/api/manage.js";
import  skCommon  from "./skCommon";
const skArea = {
  // // 亲密度
  getAreaDuration(classId,areaType) {
    return new Promise((resolve, reject) => {
      let params = {
        startDate: skCommon.getDate(0),
        endDate: skCommon.getDate(0),
        areaType: areaType,
        classId: classId ? classId : null,
      }
      let url = "/management/skArea/getStudentRelationship";
      let dataList = []
      getAction(url, params).then((res) => {
        if (res.success) {
          let data = res.result.studentData.map(item => {
            return {
              name: item.studentName,
              id: item.studentId
            }
          })
          let linksData = res.result.studentLinks.map(item => {
            return {
              target: item.target,
              source: item.source,
              relationshipRate: Number(item.relationshipRate * 100).toFixed(0) + "%"
            }
          })
          dataList.push(data, linksData)
          resolve(dataList)
        } else {
          this.$message.warning(res.message);
        }
      }).catch((error) => {
        console.error(error);
      })
    })
  },
  // 查询学生最喜欢的区域
  getInDoorStuent(classId,areaType){
    return new Promise((resolve, reject) => {
      let params = {
        areaType:areaType,
        classId:classId?classId:null,
        startDate:skCommon.getDate(0),
        endDate:skCommon.getDate(0),
      }
      let url = "/management/skArea/getAreaStudentDuration";
      let json = {
        bardata:[],
        barName:[],
        areaList:[]
      }
      getAction(url,params).then((res)=>{
        if(res.success){
          if( res.result.length > 0){
            let studentName = []
            res.result[0].studentVOList.map(item=>{
              studentName.push(item.studentName)
            })
            json.barName = studentName.sort((a,b)=>{
             return a.localeCompare(b)
            })
            res.result.map(item=>{
              let data = [];
              json.areaList.push(item.areaName);
              let list = []
              list = item.studentVOList.sort((a,b)=>{
                return a.studentName.localeCompare(b.studentName)
              })
              list.map(j=>{
                data.push(j.duration)
              })
              json.bardata.push({data})
            })
            resolve(json)
          }
        } else {
          this.$message.warning(error.message);
        }
      }).catch((error)=>{
        console.error(error);
      })
    })
  },
  // 查询受欢迎的区域
  getInDoorArea(areaType){
    return new Promise((resolve, reject) => {
      let params = {
        areaType:areaType,
        startDate:skCommon.getDate(0),
        endDate:skCommon.getDate(0),
        timeScope:"today"
      }
      let url = "management/skArea/getAreaDuration";
      let json = {
        barData:[],
        lineData:[],
        barName:[]
      }
      getAction(url,params).then((res)=>{
        if(res.success){
          let result = res.result.sort((a,b)=>{
            return b.duration - a.duration
          })
          result.slice(0,10).map(item=>{
            let name = item.areaName;
            if (areaType == 0) {
              if (item.className) {
                name =  item.className + name
              }
            }
            json.barName.push(name);
            if (item.studentCount != null) {
              json.lineData.push(item.studentCount);
            }
            if (item.duration != null) {
              json.barData.push(item.duration);
            }
          })
          resolve(json)
        } else {
          this.$message.warning(res.message);
        }
      }).catch((error)=>{
        console.error(error);
      })
    })
  },
  // 获取欢迎度
  getInDoorData(classId,areaType,sex){
    return new Promise((resolve, reject) => {
      let params = {
        sex:sex?sex:null,
        areaType:areaType,
        classId:classId?classId:null,
        timeScope:"today",
      }
      let url = "/management/skArea/getAreaDuration"
      getAction(url,params).then((res)=>{
        if(res.success){
          let result = res.result.sort((a,b)=>{
            return b.studentCount - a.studentCount
          })
          resolve(result)
        } else {
          this.$message.warning(res.message);
        }
      }).catch((error)=>{
        console.error(error);
      })
    })
  },
}
export default skArea